package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.*;
import java.util.List;

@RestController
@RequestMapping("/system/shdashboard")
public class SysShDashboardController extends BaseController {

    @Autowired
    private ISysCusService1BiService cusService1BiService;

    @Autowired
    private ISysServiceWorkOrderBiService workOrderBiService;

    @Autowired
    private ISysTsWorkOrderBiService tsWorkOrderBiService;

    @Autowired
    private ISysXsshWorkOrderBiService xsshWorkOrderBiService;

    @Autowired
    private ISysCjWorkOrderBiService cjWorkOrderBiService;

    @GetMapping("/listShzsSummary")
    public AjaxResult listShzsSummary(SysCusService1Bi vo){

        String sql = "select wk_year as wkYear,count(*) as summary from cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += " group by 1 order by wk_year asc";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
        List<String> labels = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    labels.add(map.get("wkYear").toString());
                    datas.add(Integer.parseInt(map.get("summary").toString()));
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("labels",labels);
        res.put("datas",datas);
        return AjaxResult.success(res);
    }

    @GetMapping("/listShwtSummary")
    public AjaxResult listShwtSummary(SysCusService1Bi vo){
        String sql = "select gid as name,count(*) as `value` from cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += " group by 1";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
//        List<Map<String,Object>> list = cusService1BiService.selectShwtSummary();
        List<Map<String,Object>> datas = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    datas.add(map);
                    Random random = new Random();
                    // 生成随机 RGB 值
                    int red = random.nextInt(256);
                    int green = random.nextInt(256);
                    int blue = random.nextInt(256);
                    String hexColor = String.format("#%02X%02X%02X", red, green, blue);
                    colors.add(hexColor);
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("datas",datas);
        res.put("colors",colors);
        return AjaxResult.success(res);
    }

    @GetMapping("/listShlxSummary")
    public AjaxResult listShlxSummary(SysCusService1Bi vo){
        String sql = "select sh_type as name,count(*) as `value` from cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += "  group by 1";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
//        List<Map<String,Object>> list = cusService1BiService.selectShlxSummary();
        List<Map<String,Object>> datas = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    datas.add(map);
                    Random random = new Random();
                    // 生成随机 RGB 值
                    int red = random.nextInt(256);
                    int green = random.nextInt(256);
                    int blue = random.nextInt(256);
                    String hexColor = String.format("#%02X%02X%02X", red, green, blue);
                    colors.add(hexColor);
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("datas",datas);
        res.put("colors",colors);
        return AjaxResult.success(res);
    }

    @GetMapping("/listShcpSummary")
    public AjaxResult listShcpSummary(SysCusService1Bi vo){
        String sql = "select zmpz as name,count(*) as `value` from cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += " group by 1";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
//        List<Map<String,Object>> list = cusService1BiService.selectShcpSummary();
        List<Map<String,Object>> datas = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    datas.add(map);
                    Random random = new Random();
                    // 生成随机 RGB 值
                    int red = random.nextInt(256);
                    int green = random.nextInt(256);
                    int blue = random.nextInt(256);
                    String hexColor = String.format("#%02X%02X%02X", red, green, blue);
                    colors.add(hexColor);
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("datas",datas);
        res.put("colors",colors);
        return AjaxResult.success(res);
    }

    @GetMapping("/listAreaSummary")
    public AjaxResult listAreaSummary(SysCusService1Bi vo){
        String sql = "select area,count(*) as summary from cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += " group by area order by summary";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
        List<String> labels = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        List<String> colors = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    labels.add(map.get("area").toString());
                    datas.add(Integer.parseInt(map.get("summary").toString()));
                    Random random = new Random();
                    // 生成随机 RGB 值
                    int red = random.nextInt(256);
                    int green = random.nextInt(256);
                    int blue = random.nextInt(256);
                    String hexColor = String.format("#%02X%02X%02X", red, green, blue);
                    colors.add(hexColor);
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("labels",labels);
        res.put("datas",datas);
        res.put("colors",colors);
        return AjaxResult.success(res);
    }

    @GetMapping("/listCusService")
    public AjaxResult listCusService(SysCusService1Bi vo){
        String sql = "SELECT wk_year,sh_type,area,zmpz," +
                "CASE WHEN gid = '不长' THEN 1 ELSE 0 END AS bz," +
                "CASE WHEN gid = '服务' THEN 1 ELSE 0 END AS fw," +
                "CASE WHEN gid = '根系不好' THEN 1 ELSE 0 END AS gxbh," +
                "CASE WHEN gid = '黄叶' THEN 1 ELSE 0 END AS hy," +
                "CASE WHEN gid = '货版不对' THEN 1 ELSE 0 END AS hbbd," +
                "CASE WHEN gid = '苗大' THEN 1 ELSE 0 END AS md," +
                "CASE WHEN gid = '苗弱' THEN 1 ELSE 0 END AS mr," +
                "CASE WHEN gid = '苗小' THEN 1 ELSE 0 END AS mx," +
                "CASE WHEN gid = '难拔苗' THEN 1 ELSE 0 END AS nbm," +
                "CASE WHEN gid = '难拔起来' THEN 1 ELSE 0 END AS nbql," +
                "CASE WHEN gid = '死苗' THEN 1 ELSE 0 END AS sm," +
                "CASE WHEN gid = '提前上花' THEN 1 ELSE 0 END AS tqsh," +
                "CASE WHEN gid = '土球散' THEN 1 ELSE 0 END AS tqs," +
                "CASE WHEN gid = '异常' THEN 1 ELSE 0 END AS yc," +
                "CASE WHEN gid = '其他' THEN 1 ELSE 0 END AS qt ";
            sql += " FROM cus_service1_bi where 1=1 ";
        if(vo.getArea()!=null){
            sql += " and area='"+vo.getArea()+"'";
        }
        if(vo.getWkYear()!=null){
            sql += " and wk_year='"+vo.getWkYear()+"'";
        }
        sql += " order by wk_year desc";
        List<Map<String,Object>> list = cusService1BiService.selectSummaryInfo(sql);
//        List<Map<String,Object>> list = cusService1BiService.selectCusServiceList();
        return AjaxResult.success(list);
    }


    @GetMapping("/listKfhfOrder")
    public AjaxResult listKfhfOrder(SysServiceWorkOrderBi sysServiceWorkOrderBi){
        List<Map<String,Object>> list = workOrderBiService.selectKfhfOrders();
        List<String> labels = new ArrayList<>();
        List<Map<String,Object>> series = new ArrayList<>();
        List<Integer> esxh = new ArrayList<>();
        List<Integer> hmq = new ArrayList<>();
        List<Integer> ymq = new ArrayList<>();
        List<Integer> szq = new ArrayList<>();
        List<Integer> shq = new ArrayList<>();
        List<Integer> zh = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    if(map.get("rq").toString().equalsIgnoreCase("0")){
                        labels.add("Null");
                    }else{
                        labels.add(map.get("rq").toString());
                    }

                    esxh.add(Integer.parseInt(map.get("esxh").toString()));
                    hmq.add(Integer.parseInt(map.get("hmq").toString()));
                    ymq.add(Integer.parseInt(map.get("ymq").toString()));
                    szq.add(Integer.parseInt(map.get("szq").toString()));
                    shq.add(Integer.parseInt(map.get("shq").toString()));
                    zh.add(Integer.parseInt(map.get("zh").toString()));
                }
            }
        }
        Map<String,Object> essmap = new HashMap<>();
        essmap.put("name","二十四小时");
        essmap.put("type","line");
        essmap.put("stack","Total");
        essmap.put("data",esxh);
        series.add(essmap);
        Map<String,Object> hmqmap = new HashMap<>();
        hmqmap.put("name","缓苗期");
        hmqmap.put("type","line");
        hmqmap.put("stack","Total");
        hmqmap.put("data",hmq);
        series.add(hmqmap);
        Map<String,Object> ymqmap = new HashMap<>();
        ymqmap.put("name","幼苗期");
        ymqmap.put("type","line");
        ymqmap.put("stack","Total");
        ymqmap.put("data",ymq);
        series.add(ymqmap);
        Map<String,Object> szqmap = new HashMap<>();
        szqmap.put("name","生长期");
        szqmap.put("type","line");
        szqmap.put("stack","Total");
        szqmap.put("data",szq);
        series.add(szqmap);
        Map<String,Object> shqmap = new HashMap<>();
        shqmap.put("name","上花期");
        shqmap.put("type","line");
        shqmap.put("stack","Total");
        shqmap.put("data",shq);
        series.add(shqmap);
        Map<String,Object> zhmap = new HashMap<>();
        zhmap.put("name","总和");
        zhmap.put("type","line");
        zhmap.put("stack","Total");
        zhmap.put("data",zh);
        series.add(zhmap);
        Map<String,Object> res = new HashMap<>();
        res.put("labels",labels);
        res.put("series",series);
        return AjaxResult.success(res);
    }

    @GetMapping("/listKfhfOrdersInfo")
    public TableDataInfo listKfhfOrdersInfo(SysServiceWorkOrderBi vo){
        if(vo.getZhouqi()!=null){
            if(vo.getZhouqi().equalsIgnoreCase("总和")){
                vo.setZhouqi(null);
            }
        }
        startPage();
        List<Map<String,Object>> list = workOrderBiService.selectKfhfOrdersInfo(vo);
        return getDataTable(list);
    }

    @GetMapping("/listKftsOrdersInfo")
    public TableDataInfo listKftsOrdersInfo(SysTsWorkOrderBi vo){
        if(vo.getWtfl()!=null){
            if(vo.getWtfl().equalsIgnoreCase("总和")){
                vo.setWtfl(null);
            }
        }
        startPage();
        List<Map<String,Object>> list = tsWorkOrderBiService.selectKftsOrdersInfo(vo);
        return getDataTable(list);
    }

    @GetMapping("/listKhtsOrder")
    public AjaxResult listKhtsOrder(){
        List<Map<String,Object>> list = tsWorkOrderBiService.selectKhtsOrder();
        List<String> labels = new ArrayList<>();
        List<Map<String,Object>> series = new ArrayList<>();
        List<Integer> sm = new ArrayList<>();
        List<Integer> fw = new ArrayList<>();
        List<Integer> yqjf = new ArrayList<>();
        List<Integer> hbbd = new ArrayList<>();
        List<Integer> qt = new ArrayList<>();
        List<Integer> zh = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    labels.add(map.get("wk_year").toString());
                    sm.add(Integer.parseInt(map.get("sm").toString()));
                    fw.add(Integer.parseInt(map.get("fw").toString()));
                    yqjf.add(Integer.parseInt(map.get("yqjf").toString()));
                    hbbd.add(Integer.parseInt(map.get("hbbd").toString()));
                    qt.add(Integer.parseInt(map.get("qt").toString()));
                    zh.add(Integer.parseInt(map.get("zh").toString()));
                }
            }
        }
        Map<String,Object> smmap = new HashMap<>();
        smmap.put("name","死苗");
        smmap.put("type","line");
        smmap.put("stack","Total");
        smmap.put("data",sm);
        series.add(smmap);
        Map<String,Object> fwmap = new HashMap<>();
        fwmap.put("name","服务");
        fwmap.put("type","line");
        fwmap.put("stack","Total");
        fwmap.put("data",fw);
        series.add(fwmap);
        Map<String,Object> yqjfmap = new HashMap<>();
        yqjfmap.put("name","延期交付");
        yqjfmap.put("type","line");
        yqjfmap.put("stack","Total");
        yqjfmap.put("data",yqjf);
        series.add(yqjfmap);
        Map<String,Object> hbbdqmap = new HashMap<>();
        hbbdqmap.put("name","货不对版");
        hbbdqmap.put("type","line");
        hbbdqmap.put("stack","Total");
        hbbdqmap.put("data",hbbd);
        series.add(hbbdqmap);
        Map<String,Object> qtmap = new HashMap<>();
        qtmap.put("name","NULL");
        qtmap.put("type","line");
        qtmap.put("stack","Total");
        qtmap.put("data",qt);
        series.add(qtmap);
        Map<String,Object> zhmap = new HashMap<>();
        zhmap.put("name","总和");
        zhmap.put("type","line");
        zhmap.put("stack","Total");
        zhmap.put("data",zh);
        series.add(zhmap);
        Map<String,Object> res = new HashMap<>();
        res.put("labels",labels);
        res.put("series",series);
        return AjaxResult.success(res);
    }

    @GetMapping("/listXsshOrder")
    public AjaxResult listXsshOrder(){
        List<Map<String,Object>> list = xsshWorkOrderBiService.selectXsshOrder();
        List<String> labels = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        if(list!=null && list.size()>0){
            for(Map<String,Object> map:list){
                if(map!=null){
                    if(map.get("wk_year")!=null){
                        labels.add(map.get("wk_year").toString());
                    }else{
                        labels.add("Null");
                    }

                    datas.add(Integer.parseInt(map.get("summary").toString()));
                }
            }
        }
        Map<String,Object> res = new HashMap<>();
        res.put("labels",labels);
        res.put("datas",datas);
        return AjaxResult.success(res);
    }

    @GetMapping("/listXsshOrdersInfo")
    public TableDataInfo listXsshOrdersInfo(SysXsshWorkOrderBi sysXsshWorkOrderBi){
        startPage();
        List<Map<String,Object>> list = xsshWorkOrderBiService.selectXsshOrdersInfo(sysXsshWorkOrderBi);
        return getDataTable(list);
    }

    @GetMapping("/listCjshSummary")
    public AjaxResult listCjshSummary(){
        List<Map<String,Object>> list = cjWorkOrderBiService.listCjshSummary();
        return AjaxResult.success(list);
    }

    @GetMapping("/listCjshOrdersInfo")
    public TableDataInfo listCjshOrdersInfo(SysCjWorkOrderBi cjWorkOrderBi){
        startPage();
        List<Map<String,Object>> list = cjWorkOrderBiService.selectCjshOrdersInfo();
        return getDataTable(list);
    }
}
